

set more off
mat drop _all




use "$path_data/data/temp/main_reg_data.dta", clear


sum math_score if grade==2
gen math_score_std = (math_score-r(mean))/r(sd) if grade==2
sum math_score if grade==4
replace math_score_std = (math_score-r(mean))/r(sd) if grade==4

gen dif_psc = math_score_std - ptsbl_overall_score

tab grade, gen(dum)
gen treatment_grade = treatment*dum1



sum Bangla if grade==2
gen Bangla_std = (Bangla-r(mean))/r(sd) if grade==2
sum Bangla if grade==4
replace Bangla_std = (Bangla-r(mean))/r(sd) if grade==4

gen dif_psc_Bangla = Bangla_std - ptsbl_overall_score


teffects psmatch (dif_psc_Bangla) (treatment student_gender age age_sq, probit)  if ptsbl_overall_score != .,atet
psmatch2 treatment student_gender age age_sq if ptsbl_overall_score != ., out(math_score_std) ties ate

gen psmattrition = 1 if _n1==.
recode psmattrition (.=0)
keep student_no psmattrition
save "$path_data/data/temp/psm_attrition.dta", replace








use "$path_data/data/temp/main_reg_data.dta", clear



keep ///
dif_PTS_cog dif_DT_score dif_DT_time dif_DT_per_min dif_cpcs dif_rosen ///
cheating wrongDT DT_score_pre DT_time_pre DT_per_min_pre DT_score_post ///
DT_time_post DT_per_min_post ptsbl_overall_score cpcs_bl rosen_bl cpcs_el ///
rosen_el ptsel_overall_score student_no


foreach i in dif_PTS_cog dif_DT_score dif_DT_time dif_DT_per_min dif_cpcs dif_rosen ///
cheating wrongDT DT_score_pre DT_time_pre DT_per_min_pre DT_score_post ///
DT_time_post DT_per_min_post ptsbl_overall_score cpcs_bl rosen_bl cpcs_el ///
rosen_el ptsel_overall_score {
rename `i' `i'_main
}

save "$path_data/data/temp/main_reg_data_for_merging.dta", replace







use "$path_data/data/temp/attrition_check.dta", clear





merge 1:1 student_no using "$path_data/data/temp/main_reg_data_for_merging.dta"

drop _merge
merge 1:1 student_no using "$path_data/data/temp/psm_attrition.dta"
drop if psmattrition==1



*** Test for DT
replace DT_per_min_pre = . if dif_DT_per_min_main == . | wrongDT_main == 1
replace DT_score_pre  = . if  dif_DT_score_main == . | wrongDT_main == 1
replace DT_time_pre  = . if dif_DT_time_main == .  | wrongDT_main == 1
replace pts_pre  = . if dif_PTS_cog_main == .
replace rosen_pre  = . if  dif_rosen_main == .
replace cpcs_pre = . if dif_cpcs_main == .

    


foreach j in n mean sd {
tabstat DT_per_min_pre DT_score_pre DT_time_pre DT_per_min_post DT_score_post DT_time_post if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr1_DT = r(StatTotal)
}

foreach j in n mean sd {
tabstat DT_per_min_pre DT_score_pre DT_time_pre DT_per_min_post DT_score_post DT_time_post if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct1_DT = r(StatTotal)
}



matrix n_dif1_DT = J(1,6,.)
forvalues i = 1/6 {
	matrix n_dif1_DT[1,`i'] = n_tr1_DT[1,`i'] + n_ct1_DT[1,`i']
}


matrix mean_dif1_DT = J(1,6,.)
forvalues i = 1/6 {
	matrix mean_dif1_DT[1,`i'] = mean_tr1_DT[1,`i'] - mean_ct1_DT[1,`i']
}


foreach dep in DT_per_min_pre DT_score_pre DT_time_pre DT_per_min_post DT_score_post DT_time_post {
	foreach s in mean se pv obs wpv {
		matrix r1_`dep'_`s' = J(1,2,.)
	}

	reg `dep' treatment, cluster(school_no)
	
	matrix r1_`dep'_temp = r(table)
	matrix r1_`dep'_r1 = e(r2)
	matrix r1_`dep'_obs = e(N)


	foreach j in 1 2 {
	* beta
	matrix r1_`dep'_mean[1,`j'] = r1_`dep'_temp[1,`j']
	* standard error
	matrix r1_`dep'_se[1,`j'] = r1_`dep'_temp[2,`j']
	* p value
	matrix r1_`dep'_pv[1,`j'] = r1_`dep'_temp[4,`j']
	}


	
	
	
	cgmwildboot_2 `dep' treatment, cluster(school_no) bootcluster(school_no) null(0 .) reps(999) seed(1)
	matrix m1_`dep'_p = e(Pvals)
	matrix m1_`dep'_b = e(b)
	matrix m1_`dep'_v = e(V)
}






foreach j in n mean sd {
tabstat pts_pre pts_post if treatment == 1 , stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr1_PTS = r(StatTotal)
}

foreach j in n mean sd {
tabstat pts_pre pts_post if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct1_PTS = r(StatTotal)
}

matrix n_dif1_PTS = J(1,6,.)
forvalues i = 1/6 {
	matrix n_dif1_PTS[1,`i'] = n_tr1_PTS[1,`i'] + n_ct1_PTS[1,`i']
}


matrix mean_dif1_PTS = J(1,6,.)
forvalues i = 1/6 {
	matrix mean_dif1_PTS[1,`i'] = mean_tr1_PTS[1,`i'] - mean_ct1_PTS[1,`i']
}


*foreach dep in DT_per_min_pre DT_score_pre DT_time_pre DT_per_min_post DT_score_post DT_time_post {
*	cgmwildboot_2 `dep' treatment, cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
*	matrix m_`dep'_p = e(Pvals)
*}

foreach dep in pts_pre pts_post {
	foreach s in mean se pv obs wpv {
		matrix r1_`dep'_`s' = J(1,2,.)
	}

	reg `dep' treatment, cluster(school_no)
	matrix r1_`dep'_temp = r(table)
	matrix r1_`dep'_r1 = e(r2)
	matrix r1_`dep'_obs = e(N)


	foreach j in 1 2 {
	* beta
	matrix r1_`dep'_mean[1,`j'] = r1_`dep'_temp[1,`j']
	* standard error
	matrix r1_`dep'_se[1,`j'] = r1_`dep'_temp[2,`j']
	* p value
	matrix r1_`dep'_pv[1,`j'] = r1_`dep'_temp[4,`j']
	}


	cgmwildboot_2 `dep' treatment, cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
	matrix m1_`dep'_p = e(Pvals)
	matrix m1_`dep'_b = e(b)
	matrix m1_`dep'_v = e(V)
}









foreach j in n mean sd {
tabstat rosen_pre rosen_post if treatment == 1 & rosen_el != . , stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr1_rosen = r(StatTotal)
}

foreach j in n mean sd {
tabstat rosen_pre rosen_post if treatment == 0 & rosen_el != ., stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct1_rosen = r(StatTotal)
}

matrix n_dif1_rosen = J(1,6,.)
forvalues i = 1/6 {
	matrix n_dif1_rosen[1,`i'] = n_tr1_rosen[1,`i'] + n_ct1_rosen[1,`i']
}



matrix mean_dif1_rosen = J(1,6,.)
forvalues i = 1/6 {
	matrix mean_dif1_rosen[1,`i'] = mean_tr1_rosen[1,`i'] - mean_ct1_rosen[1,`i']
}


*foreach dep in DT_per_min_pre DT_score_pre DT_time_pre DT_per_min_post DT_score_post DT_time_post {
*	cgmwildboot_2 `dep' treatment, cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
*	matrix m_`dep'_p = e(Pvals)
*}

foreach dep in rosen_pre rosen_post {
	foreach s in mean se pv obs wpv {
		matrix r1_`dep'_`s' = J(1,2,.)
	}

	reg `dep' treatment if rosen_el != ., cluster(school_no)

	matrix r1_`dep'_temp = r(table)
	matrix r1_`dep'_r1 = e(r2)
	matrix r1_`dep'_obs = e(N)


	foreach j in 1 2 {
	* beta
	matrix r1_`dep'_mean[1,`j'] = r1_`dep'_temp[1,`j']
	* standard error
	matrix r1_`dep'_se[1,`j'] = r1_`dep'_temp[2,`j']
	* p value
	matrix r1_`dep'_pv[1,`j'] = r1_`dep'_temp[4,`j']
	}


	cgmwildboot_2 `dep' treatment if rosen_el != ., cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
	matrix m1_`dep'_p = e(Pvals)
	matrix m1_`dep'_b = e(b)
	matrix m1_`dep'_v = e(V)
}






foreach j in n mean sd {
tabstat cpcs_pre cpcs_post if treatment == 1 & cpcs_el != ., stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr1_cpcs = r(StatTotal)
}

foreach j in n mean sd {
tabstat cpcs_pre cpcs_post if treatment == 0 & cpcs_el != ., stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct1_cpcs = r(StatTotal)
}


matrix n_dif1_cpcs = J(1,6,.)
forvalues i = 1/6 {
	matrix n_dif1_cpcs[1,`i'] = n_tr1_cpcs[1,`i'] + n_ct1_cpcs[1,`i']
}



matrix mean_dif1_cpcs = J(1,6,.)
forvalues i = 1/6 {
	matrix mean_dif1_cpcs[1,`i'] = mean_tr1_cpcs[1,`i'] - mean_ct1_cpcs[1,`i']
}


*foreach dep in DT_per_min_pre DT_score_pre DT_time_pre DT_per_min_post DT_score_post DT_time_post {
*	cgmwildboot_2 `dep' treatment, cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
*	matrix m_`dep'_p = e(Pvals)
*}

foreach dep in cpcs_pre cpcs_post {
	foreach s in mean se pv obs wpv {
		matrix r1_`dep'_`s' = J(1,2,.)
	}

	reg `dep' treatment if  cpcs_el != ., cluster(school_no)
	reg `dep' treatment if  cpcs_el != ., cluster(school_no)

	matrix r1_`dep'_temp = r(table)
	matrix r1_`dep'_r1 = e(r2)
	matrix r1_`dep'_obs = e(N)


	foreach j in 1 2 {
	* beta
	matrix r1_`dep'_mean[1,`j'] = r1_`dep'_temp[1,`j']
	* standard error
	matrix r1_`dep'_se[1,`j'] = r1_`dep'_temp[2,`j']
	* p value
	matrix r1_`dep'_pv[1,`j'] = r1_`dep'_temp[4,`j']
	}

	cgmwildboot_2 `dep' treatment if cpcs_el != ., cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
	matrix m1_`dep'_p = e(Pvals)
	matrix m1_`dep'_b = e(b)
	matrix m1_`dep'_v = e(V)
}

foreach j in n mean sd {
tabstat gender if treatment == 1 , stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr1_demo = r(StatTotal)
}

foreach j in n mean sd {
tabstat gender if treatment == 0 , stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct1_demo = r(StatTotal)
}



matrix n_dif1_demo = J(1,6,.)
forvalues i = 1/6 {
	matrix n_dif1_demo[1,`i'] = n_tr1_demo[1,`i'] + n_ct1_demo[1,`i']
}


matrix mean_dif1_demo = J(1,6,.)
forvalues i = 1/6 {
	matrix mean_dif1_demo[1,`i'] = mean_tr1_demo[1,`i'] - mean_ct1_demo[1,`i']
}


foreach dep in gender {
	foreach s in mean se pv obs wpv {
		matrix r1_`dep'_`s' = J(1,2,.)
	}

	reg `dep' treatment , cluster(school_no)
	
	matrix r1_`dep'_temp = r(table)
	matrix r1_`dep'_r1 = e(r2)
	matrix r1_`dep'_obs = e(N)


	foreach j in 1 2 {
	* beta
	matrix r1_`dep'_mean[1,`j'] = r1_`dep'_temp[1,`j']
	* standard error
	matrix r1_`dep'_se[1,`j'] = r1_`dep'_temp[2,`j']
	* p value
	matrix r1_`dep'_pv[1,`j'] = r1_`dep'_temp[4,`j']
	}

	
	cgmwildboot_2 `dep' treatment , cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
	matrix m1_`dep'_p = e(Pvals)
	matrix m1_`dep'_b = e(b)
	matrix m1_`dep'_v = e(V)
}






foreach j in n mean sd {
tabstat age_pre age_sq_pre if treatment == 1 , stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr1_demo2 = r(StatTotal)
}

foreach j in n mean sd {
tabstat age_pre age_sq_pre if treatment == 0 , stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct1_demo2 = r(StatTotal)
}



matrix n_dif1_demo2 = J(1,6,.)
forvalues i = 1/6 {
	matrix n_dif1_demo2[1,`i'] = n_tr1_demo2[1,`i'] + n_ct1_demo2[1,`i']
}


matrix mean_dif1_demo2 = J(1,6,.)
forvalues i = 1/6 {
	matrix mean_dif1_demo2[1,`i'] = mean_tr1_demo2[1,`i'] - mean_ct1_demo2[1,`i']
}


foreach dep in age_pre age_sq_pre {
	foreach s in mean se pv obs wpv {
		matrix r1_`dep'_`s' = J(1,2,.)
	}

	reg `dep' treatment , cluster(school_no)
	
	matrix r1_`dep'_temp = r(table)
	matrix r1_`dep'_r1 = e(r2)
	matrix r1_`dep'_obs = e(N)


	foreach j in 1 2 {
	* beta
	matrix r1_`dep'_mean[1,`j'] = r1_`dep'_temp[1,`j']
	* standard error
	matrix r1_`dep'_se[1,`j'] = r1_`dep'_temp[2,`j']
	* p value
	matrix r1_`dep'_pv[1,`j'] = r1_`dep'_temp[4,`j']
	}

	
	cgmwildboot_2 `dep' treatment , cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
	matrix m1_`dep'_p = e(Pvals)
	matrix m1_`dep'_b = e(b)
	matrix m1_`dep'_v = e(V)
}











forvalues j = 1/1 {

foreach dep in DT_per_min_pre DT_score_pre DT_time_pre DT_per_min_post ///
DT_score_post DT_time_post pts_pre pts_post rosen_pre rosen_post ///
cpcs_pre cpcs_post gender age_pre age_sq_pre {


forvalues item = 1/1 {
		forvalues scale = 1/1 {
		if m`j'_`dep'_p[`item', `scale']<=0.01 {
			local s_`j'_`dep'_`item'`scale' %3s "***"
		}
		else if (m`j'_`dep'_p[`item', `scale']>0.01) & (m`j'_`dep'_p[`item', `scale']<=0.05) {
			local s_`j'_`dep'_`item'`scale' %2s "**"
		}
		else if (m`j'_`dep'_p[`item', `scale']>0.05) & (m`j'_`dep'_p[`item', `scale']<=0.10) {
			local s_`j'_`dep'_`item'`scale' %1s "*"
		}
		else {
			local s_`j'_`dep'_`item'`scale'  ""
		}
	} // forvalues `scale' loop

}  // forvalues `item' loop


} // foreach `dep' loop

} // forvalues `j' loop




tempname hh2
file open `hh2' using "$pardir/tableD6.tex", write replace
file write `hh2' "" _newline
file write `hh2' "% Date: `c(current_date)'" _newline
file write `hh2' "% Time: `c(current_time)'" _newline
file write `hh2' "" _newline


file write `hh2' "\begin{table}[t!]\footnotesize" _newline
file write `hh2' "  \centering" _newline
file write `hh2' "  \caption{Baseline Balance Test Results (DID Sample) with matched sample in PSM}" _newline
file write `hh2' "\label{tab:summary_PSMmatched}" _newline
file write `hh2' "\scalebox{0.9}{" _newline
file write `hh2' "\begin{threeparttable}" _newline

file write `hh2' "\begin{tabular}{lcccc}\toprule\midrule" _newline




*file write `hh2' "\cmidrule{2-5} & &&&      \\" _newline

file write `hh2' " Dependent Variable & Treatment &  Control  & Difference & N   \\\midrule" _newline

file write `hh2' "    PTSII-C Score\textsuperscript{a}      & " %04.3f (mean_tr1_PTS[1,1]) " &   " %04.3f (mean_ct1_PTS[1,1]) " & " %04.3f (mean_dif1_PTS[1,1]) `s_1_pts_pre_11' " & " (n_dif1_PTS[1,1]) " \\" _newline
file write `hh2' "    									    & [" %04.3f (sd_tr1_PTS[1,1]) "] &   [" %04.3f (sd_ct1_PTS[1,1]) "] & (" %04.3f (r1_pts_pre_se[1,1])              ") &                      \\" _newline


file write `hh2' "    DT Score per min\textsuperscript{b}   & " %04.3f (mean_tr1_DT[1,1]) " &   " %04.3f (mean_ct1_DT[1,1]) " & " %04.3f (mean_dif1_DT[1,1]) `s_1_DT_per_min_pre_11' " & " (n_dif1_DT[1,1]) " \\" _newline
file write `hh2' "    									    & [" %04.3f (sd_tr1_DT[1,1]) "] &   [" %04.3f (sd_ct1_DT[1,1]) "] & (" %04.3f (r1_DT_per_min_pre_se[1,1])             ") &                     \\" _newline


file write `hh2' "    DT Score                              & " %04.3f (mean_tr1_DT[1,2]) " &   " %04.3f (mean_ct1_DT[1,2]) " & " %04.3f (mean_dif1_DT[1,2]) `s_1_DT_score_pre_11' " & " (n_dif1_DT[1,2]) " \\" _newline
file write `hh2' "    									    & [" %04.3f (sd_tr1_DT[1,2]) "] &   [" %04.3f (sd_ct1_DT[1,2]) "] & (" %04.3f (r1_DT_score_pre_se[1,1])             ") &                     \\" _newline


file write `hh2' "    DT Time                               & " %04.3f (mean_tr1_DT[1,3]) " &   " %04.3f (mean_ct1_DT[1,3]) " & " %04.3f (mean_dif1_DT[1,3]) `s_1_DT_time_pre_11' " & " (n_dif1_DT[1,3]) " \\" _newline
file write `hh2' "    									    & [" %04.3f (sd_tr1_DT[1,3]) "] &   [" %04.3f (sd_ct1_DT[1,3]) "] & (" %04.3f (r1_DT_time_pre_se[1,1])             ") &                     \\" _newline


file write `hh2' "    RSES Index\textsuperscript{c}               & " %04.3f (mean_tr1_rosen[1,1]) " &   " %04.3f (mean_ct1_rosen[1,1]) " & " %04.3f (r1_rosen_pre_mean[1,1]) `s_1_rosen_pre_11' " & " (n_dif1_rosen[1,1]) " \\" _newline
file write `hh2' "    									    & [" %04.3f (sd_tr1_rosen[1,1]) "] &   [" %04.3f (sd_ct1_rosen[1,1]) "] & (" %04.3f (r1_rosen_pre_se[1,1])                ") &                        \\" _newline


file write `hh2' "    CPCS Index\textsuperscript{c}               & " %04.3f (mean_tr1_cpcs[1,1]) " &   " %04.3f (mean_ct1_cpcs[1,1]) " & " %04.3f (r1_cpcs_pre_mean[1,1]) `s_1_cpcs_pre_11' " & " (n_dif1_cpcs[1,1]) " \\" _newline
file write `hh2' "    									    & [" %04.3f (sd_tr1_cpcs[1,1]) "] &   [" %04.3f (sd_ct1_cpcs[1,1]) "] & (" %04.3f (r1_cpcs_pre_se[1,1])               ") &                       \\" _newline


file write `hh2' "    Female                                & " %04.3f (mean_tr1_demo[1,1]) " &   " %04.3f (mean_ct1_demo[1,1]) " & " %04.3f (mean_dif1_demo[1,1]) `s_1_gender_11' " & " (n_dif1_demo[1,1]) " \\" _newline
file write `hh2' "    									    & [" %04.3f (sd_tr1_demo[1,1]) "] &   [" %04.3f (sd_ct1_demo[1,1]) "] & (" %04.3f (r1_gender_se[1,1])               ") &                       \\" _newline


file write `hh2' "    Age                                   & " %04.3f (mean_tr1_demo2[1,1]) " &   " %04.3f (mean_ct1_demo2[1,1]) " & " %04.3f (mean_dif1_demo2[1,1]) `s_1_age_pre_11' " & " (n_dif1_demo2[1,2]) " \\ " _newline
file write `hh2' "    									    & [" %04.3f (sd_tr1_demo2[1,1]) "] &   [" %04.3f (sd_ct1_demo2[1,1]) "] & (" %04.3f (r1_age_pre_se[1,1])               ") &                       \\" _newline


file write `hh2' "    Age Squared                            & " %04.3f (mean_tr1_demo2[1,2]) " &   " %04.3f (mean_ct1_demo2[1,2]) " & " %04.3f (mean_dif1_demo2[1,2]) `s_1_age_sq_pre_1' " & " (n_dif1_demo2[1,2]) " \\ " _newline
file write `hh2' "    									    & [" %04.3f (sd_tr1_demo2[1,2]) "] &   [" %04.3f (sd_ct1_demo2[1,2]) "] & (" %04.3f (r1_age_sq_pre_se[1,1])               ") &                       \\\bottomrule" _newline







file write `hh2' "\end{tabular}" _newline
file write `hh2' "\end{threeparttable}" _newline
file write `hh2' "}" _newline
file write `hh2' "\label{tab:addlabel}%" _newline
file write `hh2' "\end{table}" _newline

file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline

file close `hh2'



